package com.woniuxy.woniuticket.ordermodule.utils;

import com.woniuxy.woniuticket.movieinfomodule.dao.UsersInfoDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaHallDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaMoviesDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaScheduleDAO;
import com.woniuxy.woniuticket.moviemodule.dao.CinemaSeatsDAO;
import com.woniuxy.woniuticket.ordermodule.constant.OrderConstant;
import com.woniuxy.woniuticket.ordermodule.dto.OrderDTO;
import com.woniuxy.woniuticket.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wu xianbo on 2019/6/1
 * 该工具类用于封装订单详情的信息到OrderDTO中
 */
@Component
public class Order_InfoWrapperUtil {

    @Resource
    private UsersInfoDAO usersInfoDAO;   //用户信息详情dao
    @Resource
    private CinemaScheduleDAO cinemaScheduleDAO;  //排片信息dao
    @Resource
    private CinemaMoviesDAO cinemaMoviesDAO;   //影片dao
    @Resource
    private CinemaHallDAO cinemaHallDAO;   //影厅信息dao
    @Resource
    private CinemaSeatsDAO cinemaSeatsDAO;  //座位信息dao

    //封装订单的各种关联信息
    public OrderDTO wrapperOrderInfo(OrderDTO orderDTO, CinemaOder cinemaOder){
        CinemaSchedule cinemaSchedule = null;
        CinemaMovies cinemaMovies = null;
        CinemaHall cinemaHall = null;
        List<CinemaSeats> cinemaSeatsList = new ArrayList<>();
        //查询排片信息
        cinemaSchedule = cinemaScheduleDAO.selectByPrimaryKey(cinemaOder.getScheduleId());
        //查询影片信息
        cinemaMovies = cinemaMoviesDAO.selectByPrimaryKey(cinemaSchedule.getMoviesId());
        //查询影厅信息
        cinemaHall = cinemaHallDAO.selectByPrimaryKey(cinemaSchedule.getHallId());
        //根据订单id查询中间表查询座次信息。如果未支付则不查询座位
        if(cinemaOder.getOderstatus().equals(OrderConstant.OK_PAY) || cinemaOder.getOderstatus().equals(OrderConstant.HAVE_CHANGEED)){
            cinemaSeatsList = cinemaSeatsDAO.findSeatsByOrderNumber(cinemaOder.getOrdernum());
            orderDTO.setSeatList(cinemaSeatsList);
        }
        BeanUtils.copyProperties(cinemaOder,orderDTO);       //复制订单信息
        BeanUtils.copyProperties(cinemaSchedule,orderDTO);   //复制排片信息
        BeanUtils.copyProperties(cinemaMovies,orderDTO);     //复制影片信息
        orderDTO.setMovieId(cinemaMovies.getId());           //获取影片id
        BeanUtils.copyProperties(cinemaHall,orderDTO);       //复制影厅信息
        return orderDTO;
    }
}
